import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestUtils {
	List<Object> list = new ArrayList<Object>();
	List<Object> modList = new ArrayList<Object>();

	public void showList() {
		System.out.println("List : " + list);
	}

	public List<Object> AddList() {
		list = new ArrayList<Object>();
		list.add("A");
		list.add("B");
		list.add("A");
		list.add("C");
		list.add("A");
		list.add("D");
		return list;
	}

	public List<Object> removeDuplicate(List<Object> origList) {
		List<Object> list = new ArrayList<Object>(origList);
		Iterator<Object> it = origList.iterator();
		while (it.hasNext()) {
			for (Object curr1 : origList) {
				if (it.next().equals(curr1)) {
					list.add(curr1);
				}

			}
		}
		origList.retainAll(list);
		return origList;
	}

	void printFloydsTriangle() {
		int n = 5, c, d, num = 0;

		System.out.println("Floyd's triangle :-");

		for (c = 1; c <= n; c++) {
			for (d = 1; d <= c; d++) {
				System.out.print(num + " ");
				num++;
			}

			System.out.println();
		}
	}

	void armstrongNumber(int n) {

		int temp = 0, r = 0, sum = 0;
		for (int i = 0; i <= n; i++) {
			temp = i;
			while (temp != 0) {
				r = temp % 10;
				sum = sum + r * r * r;
				temp = temp / 10;
			}

			if (i == sum)
				System.out.println("Armstrong number: " + i);
			r = 0;
			sum = 0;
		}

	}

	public void palindrome(int n) {

		for (int i = 0; i <= n; i++) {
			int rev = 0;
			int num1 = i;
			while (num1 > 0) {
				int rem = num1 % 10;
				rev = rev * 10 + rem;
				num1 = num1 / 10;
			}
			if (i == rev) {
				System.out.println("Palindrom num:" + i);
			}
		}
	}

	public boolean stringPalindrome(String str) {
		boolean palindrome = false;
		for (int i = 0; i < str.length() - 1; i++) {
			if (str.charAt(i) == str.charAt(str.length() - i - 1)) {
				palindrome = true;
			}
		}

		return palindrome;
	}

	public String reverseString(String orig) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < orig.length(); i++) {
			buf.append(orig.charAt(orig.length() - 1 - i));
		}

		return buf.toString();

	}

	public void readFile(String fileName) {
		File file = new File(fileName);
		BufferedInputStream br = null;
		if (!file.exists()) {
			System.out.println("File does not exhist..");
		}
		try {
			br = new BufferedInputStream(new FileInputStream(file));
			InputStreamReader in = new InputStreamReader(br);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String args[]) {
		TestUtils obj = new TestUtils();

		List<Object> origlist = obj.AddList();
		obj.showList();
		// List<Object> modList = obj.removeDuplicate(origlist);
		// System.out.println("modList :"+modList);
		String s = " kjsghdkj mhdkshd khasdkh kdhd .   ";
		s.replaceAll("\\s+$", "");// right trim
		s.replaceAll("^\\s+", ""); // left trim
		obj.printFloydsTriangle();
		System.out.println(obj.reverseString("ramesh"));
		// obj.palindrome(1000);
		// obj.armstrongNumber(10000);
		System.out.println(obj.stringPalindrome("atat"));

		// FileOperation
		/*
		 * PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
		 * writer.println("The first line");
		 * 
		 * FileOutputStream out = new FileOutputStream("the-file-name");
		 * out.write(dataToWrite);
		 * 
		 * Writer writer = null;
		 * 
		 * try { writer = new BufferedWriter(new OutputStreamWriter( new
		 * FileOutputStream("filename.txt"), "utf-8"));
		 * writer.writeln("Something");
		 * 
		 * 
		 * try { File file = new File("example.txt"); BufferedWriter output =
		 * new BufferedWriter(new FileWriter(file)); output.write(text);
		 * output.close();
		 */

	}
}

class Emp {
	int empId;

	Emp(int id) {
		super();
		this.empId = id;
	}

	public int hashCode() {
		return this.empId;
	}

	@Override
	public boolean equals(Object e) {
		System.out.println("Inside equals");
		Emp e1 = (Emp) e;
		if (this.empId == e1.empId)
			return true;
		else
			return false;
	}
}

/*
 * JAXB tutorial What is JAXB? JAXB stands for Java architecture for XML
 * binding.It is used to convert XML to java object and java object to XML.JAXB
 * defines an API for reading and writing Java objects to and from XML
 * documents.Unlike SAX and DOM,we don't need to be aware of XML parsing
 * techniques.
 * 
 * There are two operations you can perform using JAXB
 * 
 * Marshalling :Converting a java object to XML UnMarshalling :Converting a XML
 * to java object
 * 
 * JAXB Tutorial We will create a java program to marshal and unmarshal. For
 * Marshalling:
 * 
 * For Unmarshalling: Java program: With the help of annotations and API
 * provided by JAXB,converting a java object to XML and vice versa become very
 * easy. 1.Country.java A Java object which will be used to convert to and from
 * XML Create Country.java in src->org.arpit.javapostsforlearning.jaxb
 * 
 * package org.arpit.javapostsforlearning.jaxb;
 * 
 * import java.util.ArrayList;
 * 
 * import javax.xml.bind.annotation.XmlElement; import
 * javax.xml.bind.annotation.XmlElementWrapper; import
 * javax.xml.bind.annotation.XmlRootElement; import
 * javax.xml.bind.annotation.XmlType;
 * 
 * //Below annotation defines root element of XML file
 * 
 * @XmlRootElement //You can define order in which elements will be created in
 * XML file //Optional
 * 
 * @XmlType(propOrder = { "countryName", "countryPopulation", "listOfStates"})
 * public class Country {
 * 
 * private String countryName; private double countryPopulation;
 * 
 * private ArrayList<state> listOfStates; public Country() {
 * 
 * } public String getCountryName() { return countryName; }
 * 
 * @XmlElement public void setCountryName(String countryName) { this.countryName
 * = countryName; } public double getCountryPopulation() { return
 * countryPopulation; }
 * 
 * @XmlElement public void setCountryPopulation(double countryPopulation) {
 * this.countryPopulation = countryPopulation; }
 * 
 * 
 * public ArrayList<state> getListOfStates() { return listOfStates; }
 * 
 * // XmLElementWrapper generates a wrapper element around XML representation
 * 
 * @XmlElementWrapper(name = "stateList") // XmlElement sets the name of the
 * entities in collection
 * 
 * @XmlElement(name = "state") public void setListOfStates(ArrayList<state>
 * listOfStates) { this.listOfStates = listOfStates; }
 * 
 * }
 * 
 * @XmlRootElement:This annotation defines root element of XML file.
 * 
 * @XmlType(propOrder = {"list of attributes in order"}):This is used to define
 * order of elements in XML file.This is optional.
 * 
 * @XmlElement:This is used to define element in XML file.It sets name of
 * entity. @XmlElementWrapper(name = "name to be given to that wrapper"):It
 * generates a wrapper element around XML representation.E.g.In above example,
 * it will generate <stateList> around each <state> element 2.State.java
 * 
 * package org.arpit.javapostsforlearning.jaxb;
 * 
 * import javax.xml.bind.annotation.XmlRootElement;
 * 
 * //Below statement means that class "Country.java" is the root-element of our
 * example
 * 
 * @XmlRootElement(namespace = "org.arpit.javapostsforlearning.jaxb.Country")
 * public class State {
 * 
 * 
 * private String stateName; long statePopulation;
 * 
 * public State() {
 * 
 * } public State(String stateName, long statePopulation) { super();
 * this.stateName = stateName; this.statePopulation = statePopulation; }
 * 
 * public String getStateName() { return stateName; }
 * 
 * public void setStateName(String stateName) { this.stateName = stateName; }
 * 
 * public long getStatePopulation() { return statePopulation; }
 * 
 * public void setStatePopulation(long statePopulation) { this.statePopulation =
 * statePopulation; } }
 * 
 * 3.JAXBJavaToXml.java
 * 
 * package org.arpit.javapostsforlearning.jaxb;
 * 
 * import java.io.File; import java.util.ArrayList;
 * 
 * import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException;
 * import javax.xml.bind.Marshaller;
 * 
 * public class JAXBJavaToXml { public static void main(String[] args) {
 * 
 * // creating country object Country countryIndia=new Country();
 * countryIndia.setCountryName("India");
 * countryIndia.setCountryPopulation(5000000);
 * 
 * // Creating listOfStates ArrayList<state> stateList=new ArrayList<state>();
 * State mpState=new State("Madhya Pradesh",1000000); stateList.add(mpState);
 * State maharastraState=new State("Maharastra",2000000);
 * stateList.add(maharastraState);
 * 
 * countryIndia.setListOfStates(stateList);
 * 
 * try {
 * 
 * // create JAXB context and initializing Marshaller JAXBContext jaxbContext =
 * JAXBContext.newInstance(Country.class); Marshaller jaxbMarshaller =
 * jaxbContext.createMarshaller();
 * 
 * // for getting nice formatted output
 * jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
 * 
 * //specify the location and name of xml file to be created File XMLfile = new
 * File("C:\\arpit\\CountryRecord.xml");
 * 
 * // Writing to XML file jaxbMarshaller.marshal(countryIndia, XMLfile); //
 * Writing to console jaxbMarshaller.marshal(countryIndia, System.out);
 * 
 * } catch (JAXBException e) { // some exception occured e.printStackTrace(); }
 * 
 * } }
 * 
 * After running above program,you will get following output Console output:
 * 
 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <country
 * xmlns:ns2="org.arpit.javapostsforlearning.jaxb.Country">
 * <countryName>India</countryName>
 * <countryPopulation>5000000.0</countryPopulation> <stateList> <state>
 * <stateName>Madhya Pradesh</stateName>
 * <statePopulation>1000000</statePopulation> </state> <state>
 * <stateName>Maharastra</stateName> <statePopulation>2000000</statePopulation>
 * </state> </stateList> </country>
 * 
 * Now we will read above generated XML and retrieve country object from it.
 * 4.JAXBXMLToJava.java
 * 
 * package org.arpit.javapostsforlearning.jaxb;
 * 
 * import java.io.File; import java.util.ArrayList; import
 * javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import
 * javax.xml.bind.Unmarshaller;
 * 
 * public class JAXBXMLToJava { public static void main(String[] args) {
 * 
 * try {
 * 
 * // create JAXB context and initializing Marshaller JAXBContext jaxbContext =
 * JAXBContext.newInstance(Country.class);
 * 
 * Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
 * 
 * // specify the location and name of xml file to be read File XMLfile = new
 * File("C:\\arpit\\CountryRecord.xml");
 * 
 * // this will create Java object - country from the XML file Country
 * countryIndia = (Country) jaxbUnmarshaller.unmarshal(XMLfile);
 * 
 * System.out.println("Country Name: "+countryIndia.getCountryName());
 * System.out
 * .println("Country Population: "+countryIndia.getCountryPopulation());
 * 
 * ArrayList<state> listOfStates=countryIndia.getListOfStates();
 * 
 * int i=0; for(State state:listOfStates) { i++;
 * System.out.println("State:"+i+" "+state.getStateName()); }
 * 
 * } catch (JAXBException e) { // some exception occured e.printStackTrace(); }
 * 
 * } }
 * 
 * After running above program,you will get following output Console output:
 * 
 * Country Name: India Country Population: 5000000.0 State:1 Madhya Pradesh
 * State:2 Maharastra
 * 
 * 
 * JAXB advantages:
 * 
 * It is very simple to use than DOM or SAX parser We can marshal XML file to
 * other data targets like inputStream,URL,DOM node. We can unmarshal XML file
 * from other data targets. We don't need to be aware of XML parsing techniques.
 * We don't need to access XML in tree structure always.
 * 
 * JAXB disadvantages:
 * 
 * JAXB is high layer API so it has less control on parsing than SAX or DOM. It
 * has some overhead tasks so it is slower than SAX.
 */